#include<bits/stdc++.h>
using namespace std;
int M,N;
int Arr[11][11],dpArr[11][11];


void dp(){
	dpArr[1][1]=Arr[1][1];
	for(int i=1;i<=M;i++){
		for(int j=1;j<=N;j++){
			if(i==1) dpArr[i][j]=dpArr[i][j-1]+Arr[i][j];
			else if(j==1) dpArr[i][j]=dpArr[i-1][j]+Arr[i][j];
			else dpArr[i][j]=min(dpArr[i-1][j],dpArr[i][j-1])+Arr[i][j];
		}
	}
}

int main(){
	scanf("%d %d",&M,&N);
	for(int i=1;i<=M;i++){
		for(int j=1;j<=N;j++)
		scanf("%d",&Arr[i][j]);

	dp();
	if(dpArr[M][N]>0) printf("%d",dpArr[M][N]);
	else{	
		if(dpArr[M][N-1] + Arr[M][N] >0 ) printf("%d",dpArr[M][N-1] + Arr[M][N] );
		else if (dpArr[M-1][N] + Arr[M][N] >0 ) printf("%d",dpArr[M-1][N] + Arr[M][N]);
		else puts("-1");	
	} 
	return 0;
}
